package com.ncepu.clubmanagement.exception.handler;

import com.ncepu.clubmanagement.exception.*;
import com.ncepu.clubmanagement.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.ResultMapResolver;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.lang.reflect.UndeclaredThrowableException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler{


    // 特殊的全局异常处理
    @ExceptionHandler(UndeclaredThrowableException.class)
    public Result undeclaredThrowableException(UndeclaredThrowableException ex){
        if (ex.getCause() instanceof AccessDeniedException){
            return accessDenied((AccessDeniedException)ex.getCause());
        }
        return Result.error("系统异常，请联系管理员");
    }

    @ExceptionHandler(FileUploadException.class)
    public Result fileUploadException(FileUploadException ex){
        log.warn("{}",ex.getMessage());
        return Result.error("文件上传错误,原因："+ex.getMessage()+"，联系管理员");
    }

    @ExceptionHandler(AccessDeniedException.class)
    public Result accessDenied(AccessDeniedException ex){
        log.warn("{}",ex.getMessage());
        return Result.error("权限不足");   // 这是正常情况，服务器必须正常运行
    }


    @ExceptionHandler(Exception.class)
    public Result ex(Exception ex){
        ex.printStackTrace();
        return Result.error("系统异常，请联系管理员");
    }

    @ExceptionHandler(AlreadyExistException.class)
    public Result alreadyExist(Exception ex){
        return Result.error("already exist");
    }

    @ExceptionHandler(PasswordUnsafeException.class)
    public Result handlePasswordUnsafeException(PasswordUnsafeException e){
        return Result.error("easy password");
    }

    @ExceptionHandler(NotExistException.class)
    public Result notExistException(){
        return Result.error("not exist");
    }



}
